/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.*;

/**
 * 应用程序函数通用类
 * @author SoChishun
 */
public class AppFuncUtil {

    /**
     * 提取文本中的日期
     * @param s
     * @return 
     * @version 1.0 2016-5-13
     */
    public static Date ParseDate(String s) {
        // "16年3月(yes)", "16-03(no)", "2016-03(yes)", "2016年3月(yes)"
        String[] aym = new String[2];
        Pattern pattern = Pattern.compile("(//d+)-(//d+)");
        Matcher matches = pattern.matcher(s);
        if (matches.find()) {
            aym[0] = matches.group(1);
            aym[1] = matches.group(2);
        } else {
            pattern = Pattern.compile("(//d+)年(//d+)月");
            matches = pattern.matcher(s);
            if (matches.find()) {
                aym[0] = matches.group(1);
                aym[1] = matches.group(2);
            }
        }
        if (null == aym[0]) {
            return null;
        }
        if (aym[0].length() == 2) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
            aym[0] = formatter.format(new Date()).substring(0, 2) + aym[0];
        }
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            return formatter.parse(aym[0] + "-" + aym[1]);
        } catch (Exception e) {
        }
        return null;
    }
}
